<?php
class ext_detail_penawaran_model extends MY_Model {
	var $nama_tabel = 'ext_detail_penawaran';
	
	function GetJSON($idPenawaran) {
		$ls_sql	= "	SELECT	a.id_detail_penawaran_pk, a.id_penawaran_fk, a.id_barang_fk, a.id_satuan_fk, a.jumlah_barang,
						TO_CHAR(a.tgl_dibutuhkan,'DD/MM/YYYY') tgl_dibutuhkan, a.harga_satuan,a.totalharga , a.ket_detail_penawaran
						,b.nama_satuan, c.nama_barang 

				FROM	".$this->nama_tabel." a
				left join ext_satuan b on a.id_satuan_fk = b.id_satuan_pk
				left join ext_barang c on a.id_barang_fk = c.id_barang_pk
				WHERE 	id_penawaran_fk = '".$idPenawaran."'
				ORDER BY id_penawaran_fk";

		// return json_encode( $this->paging($ls_sql) );
		$list = $this->db->query($ls_sql)->result_array();
		$this->db->select_sum('jumlah_barang');
		$this->db->select_sum('harga_satuan');
		$this->db->select_sum('totalharga');
		$this->db->where('id_penawaran_fk', $idPenawaran);
		$sum = $this->db->get('ext_detail_penawaran')->row_array();
		$footer[] = array('nama_barang' => '<b>Grand Total</b>','jumlah_barang' => $sum['jumlah_barang'], 'harga_satuan' => $sum['harga_satuan'],'totalharga' => $sum['totalharga']);
		$data = array('totalharga' => count($list), 'rows' => $list, 'footer' => $footer);
		// $list = $this->db->query($ls_sql)->result_array();
		// $data = array('total' => count($list), 'rows' => $list);
		return json_encode($data);
	}

	function GenerateId() {
		$sql = "select nvl(max(id_detail_penawaran_pk), 0)+1 max 
				from ext_detail_penawaran
				";
		$data = $this->db->query($sql)->row_array();

		return intval($data['max']);
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_detail_penawaran_pk' => $arr_rule['id_detail_penawaran_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

}
?>